package ws

import (
	"el/app/models"
	"log"

	"github.com/labstack/echo/v4"
)

func NewVisitor(c echo.Context) error {
	visitorId := c.FormValue("visitor_id")

	visitor := models.NewVisitor()
	visitor.VisitorId = visitorId
	visitor.InfoByVisitorId()

	conn, err := upgrader.Upgrade(c.Response().Writer, c.Request(), nil)
	if err != nil {
		log.Print("upgrade:", err)
		return err
	}

	for {
		var receive []byte
		messageType, receive, err := conn.ReadMessage()

		if err != nil {
			log.Print("upgrade:", err)
			return err
		}

		message <- &Message{
			conn:        conn,
			content:     receive,
			context:     c,
			messageType: messageType,
		}
	}
}
